package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.businesslogic.LocationCreateProcess;
import com.fan.shop.businesslogic.LocationDeleteProcess;
import com.fan.shop.businesslogic.LocationUpdateProcess;
import com.fan.shop.global.JacksonMapper;
import com.fan.shop.services.LocationService;

@ManagedBean(name = "locationsBean")
@RequestScoped
public class LocationsBean  extends ShopBean{

	private static Logger logger = Logger.getLogger(LocationsBean.class);

	@ManagedProperty(value = "#{locationService}")
	private LocationService locationService;

	private String tableData;
	private int columnsSize;
	private List<String> columnNames;
	
	private String selectedValue;
	
	private int editId;
	private String editCity;
	private String editState;
	private String editCountry;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		LocationsBean.logger = logger;
	}

	public LocationService getLocationService() {
		return locationService;
	}

	public void setLocationService(LocationService locationService) {
		this.locationService = locationService;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : locationService.getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : locationService.getList()) {
			try {
				data = data.append(mapper.writer(filters).writeValueAsString(
						obj));

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";

		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public int getColumnsSize() {
		columnsSize = getColumnNames().size();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = locationService.getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}
	
	
	
	public int getEditId() {
		return editId;
	}

	public void setEditId(int editId) {
		this.editId = editId;
	}

	public String getEditCity() {
		return editCity;
	}

	public void setEditCity(String editCity) {
		this.editCity = editCity;
	}

	public String getEditState() {
		return editState;
	}

	public void setEditState(String editState) {
		this.editState = editState;
	}

	public String getEditCountry() {
		return editCountry;
	}

	public void setEditCountry(String editCountry) {
		this.editCountry = editCountry;
	}

	public String editLocationAction()
	{
		LocationUpdateProcess locationUpdateProcess = new LocationUpdateProcess();
		locationUpdateProcess.setId(editId);
		locationUpdateProcess.setCity(editCity);
		locationUpdateProcess.setState(editState);
		locationUpdateProcess.setCountry(editCountry);
		locationUpdateProcess.setLocationService(locationService);
		
		locationUpdateProcess.process();
		return "locations.faces";
	}
	
	public String deleteLocationAction()
	{
		LocationDeleteProcess locationDeleteProcess = new LocationDeleteProcess();
		locationDeleteProcess.setId(editId);
		locationDeleteProcess.setLocationService(locationService);
		
		locationDeleteProcess.process();
		return "locations.faces";
	}
	
	public String createLocationAction()
	{
		LocationCreateProcess locationCreateProcess = new LocationCreateProcess();
		locationCreateProcess.setCity(editCity);
		locationCreateProcess.setState(editState);
		locationCreateProcess.setCountry(editCountry);
		locationCreateProcess.setLocationService(locationService);
		
		locationCreateProcess.process();
		return "locations.faces";
	}
	

}
